package com.toocms.jinjuhang.ui.mine.management_system.sales_income

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.center.IncomeData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.event.SingleLiveEvent

class SalesIncomeModel(application: Application, val bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {
    val organizationId by lazy {
        bundle?.getString(Constants.BundleKey.KEY_ORGANIZATION_ID) ?: ""
    }
    val income by lazy {
        bundle?.getString(Constants.BundleKey.KEY_INCOME) ?: ""
    }
    var p = 1

    val items = ObservableArrayList<MultiItemViewModel<*>>()
    val itemBinding = ItemBinding.of<MultiItemViewModel<*>> { itemBinding, _, item ->
        when (item.itemType) {
            Constants.RecyclerViewItemType.ITEM_TYPE_ZERO -> itemBinding.set(
                BR.salesIncomeTopItemModel, R.layout.item_sales_income_top
            )
            Constants.RecyclerViewItemType.ITEM_TYPE_ONE -> itemBinding.set(
                BR.salesIncomeRecordItemModel, R.layout.item_sales_income_record
            )
        }
    }


    val stopRefreshOrLoadSingleLiveEvent = SingleLiveEvent<Unit>()

    val refreshBindingCommand = BindingCommand<BindingAction>(BindingAction {
        refresh()
    })

    val loadBindingCommand = BindingCommand<BindingAction>(BindingAction {
        load()
    })

    init {
        refresh(true)
    }

    private fun refresh(isShowLoading: Boolean = false) {
        p = 1
        income(p, isShowLoading)
    }

    private fun load() {
        p++
        income(p)
    }

    private fun income(p: Int, isShowLoading: Boolean = false) {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["organization_id"] = organizationId
        params["p"] = p.toString()
        requestData(Urls.Center.INCOME,
            params,
            resultClass = IncomeData::class.java,
            isShowLoading = isShowLoading,
            result = {
                if (1 == p) {
                    items.clear()
                    items += SalesIncomeTopItemModel(this, ObservableField(income))
                }
                it.list?.forEach {
                    items += SalesIncomeRecordItemModel(this,ObservableField(it))
                }
            },
            finish = {
                stopRefreshOrLoadSingleLiveEvent.call()
            })
    }

}